<?php defined('BASEPATH') OR exit('No direct script access allowed');

require APPPATH.'/libraries/REST_Controller.php';

class Product extends REST_Controller{
 
    function __construct(){

        parent::__construct();

        $this->load->model(array('product_model','user_model'));

    }
    

    /**
     * 
     * Get one Department by Id
     * @author TuanNt
     * @link http://localhost/hrm/api/department/departmentById/id/$id/format/json
     * @param Integer $id
     * @return Json one Department
     * 
     **/
    
    public function auth_post(){
        $params = json_decode(file_get_contents('php://input'), true);

        $username = $params['username'];

        $password = $params['password'];

        $result = $this->user_model->login($username, $password);
        
        if($result){
            $sess_array = array();
            foreach($result as $row){
                
                $token = md5(uniqid(mt_rand(), true));
                if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                    $ip = $_SERVER['HTTP_CLIENT_IP'];
                } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
                } else {
                    $ip = $_SERVER['REMOTE_ADDR'];
                }

                $dietime=time()+10800;

                $data = array('key'=>$token,'level'=>1,'limit'=>1,'username'=>$username,'ipaddress'=>$ip,'date_created'=>$dietime); 
                $this->user_model->addKey($data);   
            }

            $this->response(array("token"=>$token), 200); 
        }else{
            $this->response(array('result'=>false),200); 
        }                     
    }    
}

?>